001    /* 
002     * Copyright 2005 Stephen McConnell.
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at 
007     * 
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     * 
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.util;
020    
021    import java.net.URL;
022    import java.io.InputStream;
023    import java.io.ByteArrayInputStream;
024    import java.io.ByteArrayOutputStream;
025    import java.io.FileNotFoundException;
026    import java.util.Properties;
027    import java.util.logging.LogManager;
028    
029    import net.dpml.transit.Transit;
030    
031    /**
032     * Utility class used to establish the logging configuration.
033     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
034     * @version 1.0.1
035     */
036    public class ConfigurationHandler
037    {
038        static
039        {
040            Object prefs = Transit.DPML_PREFS;
041        }
042        
043       /**
044        * Creation of the logging controller.
045        */
046        public ConfigurationHandler()
047        {
048            
049            //
050            // customize the configuration based on a properties file declared under 
051            // the 'dpml.logging.config' property
052            //
053            
054            Properties properties = new Properties();
055            String config = System.getProperty( "dpml.logging.config" );
056            if( null != config )
057            {
058                String spec = PropertyResolver.resolve( config );
059                try
060                {
061                    URL url = new URL( spec );
062                    InputStream stream = url.openStream();
063                    properties.load( stream );
064                    PropertyResolver.resolve( properties );
065                }
066                catch( FileNotFoundException e )
067                {
068                    final String error = 
069                      "Logging configuration does not exist."
070                      + "\nURI: " + spec;
071                    System.err.println( error );
072                }
073                catch( Exception e )
074                {
075                    System.out.println( "Error loading user properties: " + config );
076                    e.printStackTrace();
077                }
078            }
079            
080            //
081            // ensure that sensible defaults exist
082            //
083            
084            if( null == properties.getProperty( ".level" ) )
085            {
086                String level = getDefaultLevel();
087                properties.setProperty( ".level", level );
088            }
089            
090            if( null == properties.getProperty( "handlers" ) )
091            {
092                setProperty( properties, 
093                  "handlers", 
094                  "java.util.logging.ConsoleHandler" );
095                setProperty( properties, 
096                  "java.util.logging.ConsoleHandler.formatter", 
097                  "net.dpml.util.StandardFormatter" );
098                setProperty( properties, "java.util.logging.ConsoleHandler.level", "ALL" );
099            }
100            
101            //
102            // convert the resolved properties instance to an input stream
103            // and supply this to the log manager
104            //
105            
106            try
107            {
108                ByteArrayOutputStream out = new ByteArrayOutputStream();
109                properties.store( out, "DPML Logging Properties" );
110                byte[] bytes = out.toByteArray();
111                ByteArrayInputStream input = new ByteArrayInputStream( bytes );
112                LogManager manager = LogManager.getLogManager();
113                manager.readConfiguration( input );
114            }
115            catch( Throwable e )
116            {
117                e.printStackTrace();
118            }
119        }
120    
121        private void setProperty( Properties properties, String key, String value )
122        {
123            properties.setProperty( key, System.getProperty( key, value ) );
124        }
125        
126        private String getDefaultLevel()
127        {
128            //if( "true".equals( System.getProperty( "dpml.debug" ) ) )
129            //{
130            //    return "FINE";
131            //}
132            //else
133            //{
134                return System.getProperty( "dpml.logging.level", "INFO" ).toUpperCase();
135            //}
136        }    
137    }